/*
  The MIT License
  
  Copyright (c) 2009 smath project
  
  Permission is hereby granted, free of charge, to any person obtaining a copy
  of this software and associated documentation files (the "Software"), to deal
  in the Software without restriction, including without limitation the rights
  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  copies of the Software, and to permit persons to whom the Software is
  furnished to do so, subject to the following conditions:
  
  The above copyright notice and this permission notice shall be included in
  all copies or substantial portions of the Software.
  
  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  THE SOFTWARE.
*/
package pl.smath.expression.model;


/**
 * Exponentation e.g. x^2
 * @author Krzysztof Langner
 *
 */
public class ExponentiationTerm extends Term{

	private final Term base;
	private final Term exponent;
	
	/**
	 * Utworzenie odpowiedniego wyrazenia potegowego
	 * @param exponent1
	 * @param exponent2
	 * @return
	 */
	public static Term createExponentation(Term base, Term exponent) {
		
		Term outputTerm;
		
		if(	exponent instanceof FractionTerm && 
			NumberTerm.isOne( ((FractionTerm)exponent).getNumerator() ) ) 
		{
			outputTerm = new RootTerm(base, ((FractionTerm)exponent).getDenominator());
		}
		else if(NumberTerm.isOne(exponent)){
			outputTerm = base;
		}
		else{
			outputTerm = new ExponentiationTerm(base, exponent);
		}
		
		return outputTerm;
	}
	
	

	/**
	 * Constructor
	 * @param text
	 */
	public ExponentiationTerm(Term base, Term exponent) {
		
		super("^");
		
		this.base = base;
		this.exponent = exponent;
	}
	
	
	/**
	 * Constructor for subclasses
	 * @param text
	 */
	protected ExponentiationTerm(String text, Term base, Term exponent) {
		
		super(text);
		
		this.base = base;
		this.exponent = exponent;
	}
	
	
	/**
	 * @return tekst wyrażenia
	 */
	@Override
	public Term copy(){
		Term term = new ExponentiationTerm(getBase().copy(), getExponent().copy());
		
		term.setNegative(isNegative());
		return term;
	}

	
	
	/**
	 * Pobranie podstawy
	 */
	public Term getBase(){
		return base;
	}

	
	/**
	 * Pobranie wykladnika
	 */
	public Term getExponent(){
		return exponent;
	}
	
	
	/**
	 * Try to evaluate expression
	 */
	@Override
	public Term evaluate(){
		
		Term outputTerm = this;
		
		Term baseTerm = getBase().evaluate();
		Term expTerm = getExponent().evaluate();
		if(baseTerm instanceof NumberTerm && expTerm instanceof NumberTerm){
		
			float base = ((NumberTerm)baseTerm).getValue();
			float exp = ((NumberTerm)expTerm).getValue();
			float value = (float)Math.pow(base, exp);
			if(isNegative()){
				value = -value;
			}
			
			outputTerm = new NumberTerm(value);
		}
		
		return outputTerm;
	}
	

	/**
	 * Sprawdzenie czy 2 wyrazenia sa sobie równowazne.
	 * Czyli czy mozna postawic miedzy nimi znak = (w sensie matematycznym)
	 */
	@Override
	public boolean isAbsTheSame(Term term){
		
		if(term instanceof ExponentiationTerm){
			ExponentiationTerm et = (ExponentiationTerm)term;
			return getBase().isAbsTheSame(et.getBase()) && getExponent().isAbsTheSame(et.getExponent());
		}
		
		return false;
	}
	
	
	/**
	 * Ten term jest liczba gdy podstawa i wykladnik jest liczba
	 */
	@Override
	public boolean isNumber() {
		return base.isNumber() && exponent.isNumber();
	}

	
	/**
	 * Zamien na string
	 */
	@Override
	public String toString(){
		String output = "";
		
		if(isNegative())
			output = "-";
		
		if( !(base instanceof NumberTerm || base instanceof VariableTerm) || base.isNegative()){
			output += "(" + base.toString() + ")";
		}
		else{
			output += base.toString();
		}
		
		output += getText();
	
		if( !(exponent instanceof NumberTerm || exponent instanceof VariableTerm) || exponent.isNegative()){
			output += "(" + exponent.toString() + ")";
		}
		else{
			output += exponent.toString();
		}
		
		return output;
	
	}

	/**
	 * Transformacja wezla
	 */
	@Override
    public Term transform(ITransform trans) {
		Term outputTerm = null;
        Term term1 = null;
        Term term2 = null;

        outputTerm = trans.process(this);
        
        if(outputTerm == this){

        	term1 = base.transform(trans);
        	term2 = exponent.transform(trans);
                
            // Jezeli zmienily sie dzieci to tworzony jest nowy wezel
        	if(term1 != base || term2 != exponent){
        		outputTerm = new ExponentiationTerm(term1, term2);
        		outputTerm.setNegative(isNegative());
        	}
        }
        else{
        	outputTerm = outputTerm.transform(trans);
        }
        
        return outputTerm;
    }

}
